<?php

if ( function_exists('register_sidebars') )
	register_sidebar();

/*Custom Header Image*/
wp_enqueue_script('cropper');

function custom_admin_menu() {
		add_submenu_page('themes.php', 'Custom Header Image', 'Custom Header Image', 8, 'crop-header', 'upload_form');
		add_action('admin_head', 'custom_head_styles');		
}

function custom_head_styles(){
?>
<script type="text/javascript">
	function onEndCrop( coords, dimensions ) {
		$( 'x1' ).value = coords.x1;
		$( 'y1' ).value = coords.y1;
		$( 'x2' ).value = coords.x2;
		$( 'y2' ).value = coords.y2;
		$( 'width' ).value = dimensions.width;
		$( 'height' ).value = dimensions.height;
	}

	Event.observe( window,'load',function() {
			new Cropper.Img( 'upload',{
					ratioDim: { x: 520, y: 172 },
					displayOnInit: true,
					onEndCrop: onEndCrop
				}
			)
		}
	);
</script>
<?php
}

function upload_form(){
	global $id, $url, $file, $imagesize, $imagewidth, $imageheight, $x1, $x2, $y1, $y2, $width, $height;
	
	if ($_POST['action'] == 'default'){
		delete_option('jillwhite_header');
		echo '<div class="updated" class="updated fade"><p>The theme restored dafault!</p></div>';
	}
	

	if ($_POST['action'] == 'crop')	{
	$src_file = $_POST[attachment_id];
	$src_x = $_POST[x1];
	$src_y = $_POST[y1];
	$src_w = $_POST[x2];
	$src_h = $_POST[y2];
	$dst_w = $_POST[width];
	$dst_h = $_POST[height];
	wp_crop_image($src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false);
	}

	if ($_POST['action'] == 'save')	{

		$overrides = array('action'=>'save');

		$file = wp_handle_upload($_FILES['image'], $overrides);

		if( isset( $file[ 'error' ] ) == false )
			$file[ 'error' ] = apply_filters( "check_uploaded_file", $file[ 'error' ] );

		if ( isset($file['error']) )
			wp_die($file['error'] . '<br /><a href="' . basename(__FILE__) . '?action=upload&post=' . $post . '">'.__('Back to Image Uploading').'</a>');
	
		$url = $file['url'];
		$type = $file['type'];
		$file = $file['file'];
		$filename = basename($file);

		$imagesize = getimagesize($file);
		$imagewidth = $imagesize['0'];
		$imageheight = $imagesize['1'];

	$attachment = array(
		'post_title' => $imgtitle ? $imgtitle : $filename,
		'post_content' => $descr,
		'post_type' => 'attachment',
		'post_parent' => $post,
		'post_mime_type' => $type,
		'guid' => $url
	);
		$id = wp_insert_attachment($attachment, $file, $post_id);
		cropped_image($id);

} else
	{
?>
<?php
if (!empty($src_file)){
	echo '<div class="updated fade"><p>Your theme image has updated! <a href="'.get_bloginfo(home).'">View your site</a></p></div>';
	echo '<div class="wrap"><h2>Your New Theme Header</h2><p>';

	$src_file = get_attached_file($src_file);
	$dst_file = str_replace(basename($src_file), 'cropped-'.basename($src_file), $src_file);
	$image_file = str_replace('/home/bloggles/public_html/', '/', $dst_file);
	
	echo '<img src="'.$image_file.'">';
	echo '</p></div>';
	
	update_option('jillwhite_header',$image_file);
} else {
	$head_image = get_option('jillwhite_header',$image_file);
	if (!empty($head_image)){
		echo '<div class="wrap"><h2>Your Header Image</h2><p>This is your header image. You can upload your favorite image and crop a new image.</p><p>';
		echo '<img src="'.$head_image.'">';
		echo '</p><form action="" method="post"><input type="hidden" name="action" value="default">';
		echo '<p class="submit"><input type="submit" name="submit" value="Retore Default"></p></form></div>';
	}
}
?>
<div class="wrap">
<h2>Upload New Header Image</h2><p>Here you can upload a custom header image to be shown at the top of your blog instead of the default one. On the next screen you will be able to crop the image.</p>
<form enctype="multipart/form-data" id="uploadForm" method="POST" action="" />
<label for="upload">Choose an image from your computer:</label><br /><input type="file" id="upload" name="image" />
<input type="hidden" name="action" value="save" />
<p class="submit"><input type="submit" value="Upload Image &raquo;" /></p>
</form>

</div>
<?php
			
	}
}

function cropped_image(){
	global $id, $url, $file, $imagesize, $imagewidth, $imageheight, $x1, $x2, $y1, $y2, $width, $height, $dst_file;
?>
<div class="wrap">
<h2>View Image and crop</h2>
<form method="post" action="">
<p>Choose the part of the image you want to use as your header and submit cropped selection.</p>
<div id="testWrap">
<img src="<?php echo $url; ?>" id="upload" width="<?php echo $imagewidth; ?>" height="<?php echo $imageheight; ?>"/>
</div>
<input type="hidden" name="x1" id="x1" />
<input type="hidden" name="y1" id="y1" />
<input type="hidden" name="x2" id="x2" />
<input type="hidden" name="y2" id="y2" />
<input type="hidden" name="width" id="width" />
<input type="hidden" name="height" id="height" />
<p class="submit"><input type="submit" value="Crop Selection &raquo;" /></p>
<input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo $id; ?>" />
<input type="hidden" name="oitar" id="oitar" value="1" />
<input type="hidden" name="action" value="crop" />

</form>
</div>
<?php
}
	add_action('admin_menu', 'custom_admin_menu');
	add_action('admin_head', 'custom_head_styles');
?>